﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using Mindfor.Web.Config;
//using System.Reflection;
//using System.Web.Mvc;

//namespace Mindfor.Web.Mvc
//{
//    /// <summary>
//    /// Provides functions for controller searching.
//    /// </summary>
//    public static class ControllerFinder
//    {
//        static Dictionary<IControllersConfig, ControllerDescriptor[]> s_controllers = new Dictionary<IControllersConfig, ControllerDescriptor[]>();

//        /// <summary>
//        /// Searches all controllers in namespaces that are registered in ICmsConfig.
//        /// </summary>
//        /// <param name="config">Config to get controller namespaces from.</param>
//        /// <returns>Array of controller descriptors.</returns>
//        public static ControllerDescriptor[] FindAllControllers(this IControllersConfig config)
//        {
//            // if already searched
//            if (s_controllers.ContainsKey(config))
//                return s_controllers[config];

//            // search for controllers
//            List<ControllerDescriptor> controllers = new List<ControllerDescriptor>();
//            List<string> controllerNames = new List<string>();

//            // search all namepaces
//            foreach (string ns in config.Namespaces)
//            {
//                // search domain
//                foreach (Type t in TypeFinder.GetTypesFromCurrentDomain(ns))
//                {
//                    // if controller
//                    if (!t.IsAbstract && t.IsClass && t.GetInterfaces().Contains(typeof(IController)) && !controllerNames.Contains(t.Name))
//                    {
//                        if (!t.Name.EndsWith("Controller"))
//                            throw new ApplicationException(String.Format("Invalid controller class name: {0}. Name must ends with \"Controller\".", t.Name));
//                        controllers.Add(new ReflectedControllerDescriptor(t));
//                        controllerNames.Add(t.Name);
//                    }
//                }
//            }

//            ControllerDescriptor[] array = controllers.ToArray();
//            s_controllers.Add(config, array);
//            return array;
//        }

//        /// <summary>
//        /// Searches controller in namespaces that are registered in ICmsConfig.
//        /// </summary>
//        /// <param name="config">Config to get controller namespaces from.</param>
//        /// <param name="controllerName">ControllerName to search.</param>
//        /// <returns>Controller descriptor or <c>Null</c> if not found.</returns>
//        public static ControllerDescriptor FindController(this IControllersConfig config, string controllerName)
//        {
//            if (String.IsNullOrEmpty(controllerName))
//                throw new ArgumentNullException("controllerName");

//            string name = controllerName.ToLower();
//            return FindAllControllers(config).FirstOrDefault(r => r.ControllerName.ToLower() == name);
//        }

//        /// <summary>
//        /// Searches controller for specified action.
//        /// </summary>
//        /// <param name="config">Config to get controller namespaces from.</param>
//        /// <param name="controllerName">Controller name to search.</param>
//        /// <param name="actionName">Action name to search.</param>
//        /// <returns>Returns array of found action descriptors. If controller or actions not found returns empty array.</returns>
//        public static ActionDescriptor[] FindControllerAction(this IControllersConfig config, string controllerName, string actionName)
//        {
//            ControllerDescriptor c = FindController(config, controllerName);
//            if (c != null)
//                return c.GetCanonicalActions().Where(r => r.ActionName == actionName).ToArray();
//            return new ActionDescriptor[0];
//        }
//    }
//}
